package com.xingkong.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.xingkong.behavior.model.BehaviorModel;
import com.xingkong.behavior.model.Converter;
import com.xingkong.behavior.model.Graph;

public class XingKongServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public XingKongServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String baseFilePath = getServletContext().getRealPath("/") + "duansky"+File.separator;
		System.out.println("baseFilePath:" + baseFilePath);
		com.xingkong.behavior.model.Graph g = new com.xingkong.behavior.model.Graph();
		Converter converter = new Converter();
		String ajaxdataset=request.getParameter("ajaxdataset");
		String autoactive=request.getParameter("autoactive");
		String autoactivenumber=request.getParameter("autoactivenumber");
		String selectbest=request.getParameter("selectbest");
		String selectbestnumber=request.getParameter("selectbestnumber");
		
		String maxiterative=request.getParameter("maxiterative");
		JSONArray returnJSONArray=null;
		BehaviorModel.setRealTimeMessage("");
		if(autoactive.equals("0")){
			g = converter.readJsonFromFile(baseFilePath + "data"
					+ File.separator + ajaxdataset,true,autoactivenumber.matches("[0-9]+")&&Integer.valueOf(autoactivenumber)>0?Integer.valueOf(autoactivenumber):50,null);
			BehaviorModel bm = new BehaviorModel(g);
			if(maxiterative.matches("[0-9]+")&&Integer.valueOf(maxiterative)>0) bm.setMaxiterative(Integer.valueOf(maxiterative));
			if(selectbest.equals("1")&&selectbestnumber.matches("[0-9]+")&&Integer.valueOf(selectbestnumber)>0){
				Integer maxActiveNodes=0;
				for(int i=0;i<Integer.valueOf(selectbestnumber);i++){
					if(request.getParameter("algorithm").equals("LTM")){
						bm.LTM();
						if(bm.getCountActiveNode()>maxActiveNodes){
							maxActiveNodes=bm.getCountActiveNode();
							returnJSONArray=bm.getCascadeInfoJSON();
						}
					}
					if(request.getParameter("algorithm").equals("ICM")){
						bm.ICM();
						if(bm.getCountActiveNode()>maxActiveNodes){
							maxActiveNodes=bm.getCountActiveNode();
							returnJSONArray=bm.getCascadeInfoJSON();
						}
					}
				}
				if(maxActiveNodes==0){//防止出现没有激活的情况
					returnJSONArray=bm.getCascadeInfoJSON();
				}
				BehaviorModel.setRealTimeMessage(bm.getRealTimeMessage()+"\n"+"最多激活了"+maxActiveNodes+"个节点");
			}else{
				if(request.getParameter("algorithm").equals("LTM")) bm.LTM();
				if(request.getParameter("algorithm").equals("ICM")) bm.ICM();
				returnJSONArray=bm.getCascadeInfoJSON();
			}
		}
		if(autoactive.equals("1")){
			String[] tempList=autoactivenumber.trim().split(",");
			Integer[] activeList=new Integer[tempList.length];
			for(int i=0;i<tempList.length;i++){
				activeList[i]=Integer.valueOf(tempList[i]);
			}
			g = converter.readJsonFromFile(baseFilePath + "data"
					+ File.separator + ajaxdataset,false,0,activeList);
			BehaviorModel bm = new BehaviorModel(g);
			if(maxiterative.matches("[0-9]+")&&Integer.valueOf(maxiterative)>0) bm.setMaxiterative(Integer.valueOf(maxiterative));
			if(request.getParameter("algorithm").equals("LTM")) bm.LTM();
			if(request.getParameter("algorithm").equals("ICM")) bm.ICM();
			returnJSONArray=bm.getCascadeInfoJSON();
		}
		response.setContentType("application/x-json");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(returnJSONArray);
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
